package com.zoffy.catdiarybackend.controller;

import com.zoffy.catdiarybackend.common.BaseResponse;
import com.zoffy.catdiarybackend.common.ResultUtils;
import com.zoffy.catdiarybackend.model.dto.user.UserInformationUpdateRequest;
import com.zoffy.catdiarybackend.model.dto.user.UserLoginRequest;
import com.zoffy.catdiarybackend.model.dto.user.UserRegistryRequest;
import com.zoffy.catdiarybackend.model.vo.UserVo;
import com.zoffy.catdiarybackend.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @PostMapping("/register")
    public BaseResponse<Long> userRegistry(@RequestBody UserRegistryRequest userRegistryRequest){
        long id = userService.userRegistry(userRegistryRequest);
        return ResultUtils.success(id);
    }
    @PostMapping("/login")
    public BaseResponse<UserVo> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request){
        UserVo userVo = userService.userLogin(request, userLoginRequest);
        return ResultUtils.success(userVo);
    }
    @PostMapping("/logout")
    public BaseResponse<UserVo> userLogout(HttpServletRequest request){
        userService.userLogout(request);
        return ResultUtils.success();
    }
    @GetMapping("/current")
    public BaseResponse<UserVo> getCurrentUserVo(HttpServletRequest request){
        UserVo userVo = userService.getUserVo(request);
        return ResultUtils.success(userVo);
    }
    @GetMapping("/id")
    public BaseResponse<String> getSessionId(HttpServletRequest request){
        return ResultUtils.success(request.getSession().getId());
    }
    @PostMapping("/information")
    public BaseResponse<Boolean> updateUserInformation(HttpServletRequest request, @RequestBody UserInformationUpdateRequest userInformationUpdateRequest){
        boolean res = userService.updateUserInformation(request, userInformationUpdateRequest);
        return ResultUtils.success(res);
    }
    @PostMapping(path="/userAvatar",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public BaseResponse<Boolean> updateUserAvatar(HttpServletRequest request,@RequestPart("file") MultipartFile multipartFile) throws IOException {
        boolean res = userService.updateUserAvatar(request, multipartFile);
        return ResultUtils.success(res);

    }

}
